﻿using System.Xml.Linq;
using BattleFury.Lib.Extensions;
using BattleFury.Lib.Input;
using Microsoft.Xna.Framework;

namespace BattleFury.Lib.Controls
{
	public class ProgressSelector
		: Control
	{
		public static string PartLeftArrow = "ArrowLeft";
		public static string PartLeftArrowHighlighted = "ArrowLeftHighlighted";
		public static string PartLeftArrowClicked = "ArrowLeftClicked";
		public static string PartRightArrow = "ArrowRight";
		public static string PartRightArrowHighlighted = "ArrowRightHighlighted";
		public static string PartRightArrowClicked = "ArrowRightClicked";
		public static string PartBorderLeft = "ProgressBarLeft";
		public static string PartBorderCenter = "ProgressBarCenter";
		public static string PartBorderRight = "ProgressBarRight";

		private Part _leftArrowPart;
		private Part _leftArrowHighlightPart;
		private Part _leftArrowClickPart;
		private string _leftArrowState;

		private Part _rightArrowPart;
		private Part _rightArrowHighlightPart;
		private Part _rightArrowClickPart;
		private string _rightArrowState;

		private ProgressBar _progressBar;

		public double Value { get; set; }
		public double Minimum { get; set; }
		public double Maximum { get; set; }
		public double Increment { get; set; }
		public Color FillColor { get; set; }
		public Color BorderColor { get; set; }

		public override void Deserialize(XElement element, object callingObject)
		{
			base.Deserialize(element, callingObject);

			Value = element.GetInt("Value");
			Minimum = element.GetDouble("Minimum", 0);
			Maximum = element.GetDouble("Maximum", 100);
			Increment = element.GetDouble("Increment", 1.0);
			FillColor = element.GetColor("FillColor", Color.Black);
			BorderColor = element.GetColor("BorderColor", Color.White);
		}

		public override void Update(GameTime gameTime)
		{
			if (_leftArrowPart == null)
			{
				_leftArrowPart = Skin[PartLeftArrow];
				_rightArrowPart = Skin[PartRightArrow];
				_leftArrowHighlightPart = Skin[PartLeftArrowHighlighted];
				_rightArrowHighlightPart = Skin[PartRightArrowHighlighted];
				_leftArrowClickPart = Skin[PartLeftArrowClicked];
				_rightArrowClickPart = Skin[PartRightArrowClicked];

				var progressWidth = Size.X - (_leftArrowPart.Width + _rightArrowPart.Width);
				_progressBar = new ProgressBar
				{
					Value = Value,
					Minimum = Minimum,
					Maximum = Maximum,
					Position = Position + new Vector2(_leftArrowPart.Width, 0f),
					Size = new Vector2(progressWidth, Size.Y),
					Skin = Skin,
					Font = Font,
					FillColor = FillColor,
					BorderColor = BorderColor
				};
			}

			var input = RenderEngine.Get().Input;

			_progressBar.Update(gameTime);
			_progressBar.Value = Value;

			_leftArrowState = "Normal";
			var leftArrowRect = new Rectangle((int)Position.X, (int)Position.Y, (int)_leftArrowPart.Width, (int)_leftArrowPart.Height);
			if (input.Mouse.CurrentBounds.Intersects(leftArrowRect))
			{
				_leftArrowState = "Highlighted";
				if (input.Mouse.IsMouseButtonHeld(MouseButtons.Left))
				{
					_leftArrowState = "Clicked";
					Value--;
					if (Value < Minimum) Value = Minimum;
				}
			}

			var innerWidth = (Size.X - (_leftArrowPart.Width + _rightArrowPart.Width) + _leftArrowPart.Width);
			_rightArrowState = "Normal";
			var rightArrowRect = new Rectangle((int)(Position.X + innerWidth), (int)Position.Y, (int)_rightArrowPart.Width, (int)_rightArrowPart.Height);
			if (input.Mouse.CurrentBounds.Intersects(rightArrowRect))
			{
				_rightArrowState = "Highlighted";
				if (input.Mouse.IsMouseButtonHeld(MouseButtons.Left))
				{
					_rightArrowState = "Clicked";
					Value++;
					if (Value > Maximum) Value = Maximum;
				}
			}
		}

		public override void Draw(GameTime gameTime)
		{
			var leftArrowRect = new Rectangle((int)Position.X, (int)Position.Y, (int)_leftArrowPart.Width, (int)_leftArrowPart.Height);
			var leftArrowPart = _leftArrowPart;
			if (_leftArrowState == "Highlighted") leftArrowPart = _leftArrowHighlightPart;
			if (_leftArrowState == "Clicked") leftArrowPart = _leftArrowClickPart;
			SpriteBatch.Draw(Texture, leftArrowRect, leftArrowPart.SourceBounds, Color.White);

			var innerWidth = (Size.X - (_leftArrowPart.Width + _rightArrowPart.Width) + _leftArrowPart.Width);
			var rightArrowRect = new Rectangle((int)(Position.X + innerWidth), (int)Position.Y, (int)_rightArrowPart.Width, (int)_rightArrowPart.Height);
			var rightArrowPart = _rightArrowPart;
			if (_rightArrowState == "Highlighted") rightArrowPart = _rightArrowHighlightPart;
			if (_rightArrowState == "Clicked") rightArrowPart = _rightArrowClickPart;
			SpriteBatch.Draw(Texture, rightArrowRect, rightArrowPart.SourceBounds, Color.White);

			_progressBar.Draw(gameTime);
		}
	}
}